package com.hanson.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanson.common.constant.Constant;
import com.hanson.mapper.TbIpListMapper;
import com.hanson.pojo.vo.TbIpList;
import com.hanson.service.ITbIpListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * ip黑白名单 服务实现类
 * </p>
 *
 * @author HansonLee
 * @since 2022-08-31
 */

@Slf4j
@Service
public class TbIpListServiceImpl extends ServiceImpl<TbIpListMapper, TbIpList> implements ITbIpListService {

    @Resource
    private TbIpListMapper tbIpListMapper;

    /**
     * 分页查询tbIpList
     *
     * @param condition 数据筛选条件
     * @param page      分页对象
     * @return {@link Page< TbIpList>}      传进来的分页对象，封装了查询数据结果集
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public Page<TbIpList> list(String condition, Page<TbIpList> page) {
        QueryWrapper<TbIpList> wrapper = new QueryWrapper<TbIpList>();
        TbIpList tbIpList = new TbIpList();
        //↓↓↓↓↓↓↓↓为TbIpList查询对象设置条件set condition↓↓↓↓↓↓↓↓

        //↑↑↑↑↑↑↑↑为TbIpList查询对象设置条件set condition↑↑↑↑↑↑↑↑
        wrapper.setEntity(tbIpList);
        return super.page(page, wrapper);
    }

    /**
     * 添加新tbIpList
     *
     * @param tbIpList 需要添加的tbIpList
     * @return {@link int}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public int add(TbIpList tbIpList) {
        return baseMapper.insert(tbIpList);
    }

    /**
     * 根据条件更新tbIpList
     *
     * @param condition 新数据以及更新条件，使用tbIpList实体类封装
     * @return {@link int}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public int update(TbIpList condition) {
        //条件封装类
        UpdateWrapper<TbIpList> wrapper = new UpdateWrapper<TbIpList>();
//        wrapper.setEntity(condition);
        //封装更新条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值
        wrapper.set("type", Constant.BLACK_LIST_IP)
                .eq("ip",condition.getIp())
                .eq("status",Constant.DELETE_FLAG_CLOSE)
                .eq("type",Constant.WHITE_LIST_IP).nonEmptyOfWhere();
        return baseMapper.update(condition, wrapper);
    }

    /**
     * 根据条件删除tbIpList
     *
     * @param condition 删除条件，使用tbIpList实体类封装
     * @return {@link int}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public int delete(TbIpList condition) {
        //条件封装类
        UpdateWrapper<TbIpList> wrapper = new UpdateWrapper<TbIpList>();
        wrapper.setEntity(condition);
        //封装删除条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值

        return baseMapper.delete(wrapper);
    }

    /**
     * 根据id删除tbIpList
     *
     * @param id 删除条件，需要删除的id
     * @return {@link int}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public int deleteById(Serializable id) {
        return baseMapper.deleteById(id);
    }

    /**
     * 根据条件查询一条tbIpList
     *
     * @param condition 查询条件
     * @return {@link TbIpList}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public TbIpList selectOne(TbIpList condition) {
        QueryWrapper<TbIpList> wrapper = new QueryWrapper<>();
        wrapper.setEntity(condition);
        //封装查询条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值

        return baseMapper.selectOne(wrapper);
    }

    /**
     * 根据id查询tbIpList
     *
     * @param id id
     * @return {@link TbIpList}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public TbIpList selectById(Serializable id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据条件查询tbIpList
     *
     * @param condition 查询条件
     * @return {@link List<TbIpList>}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public List<TbIpList> selectList(TbIpList condition) {
        QueryWrapper<TbIpList> wrapper = new QueryWrapper<>();
        wrapper.setEntity(condition);
        //封装查询条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值

        return baseMapper.selectList(wrapper);
    }

    /**
     * 根据条件统计tbIpList
     *
     * @param condition 统计条件
     * @return {@link Integer}
     * @author HansonLee
     * @date 2022-08-31
     **/
    @Override
    public Integer selectCount(TbIpList condition) {
        QueryWrapper<TbIpList> wrapper = new QueryWrapper<>();
//        wrapper.setEntity(condition);
        //封装查询条件，wrapper.eq(String column,Object value),column表示数据库字段，value是字段对应的值
        wrapper.eq("ip",condition.getIp())
                .eq("status",condition.getStatus())
                .eq("type",condition.getType());
        return baseMapper.selectCount(wrapper);
    }
}
